﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xaml;
using System.Xml;

namespace AutoEditUI.UserControls
{
    /// <summary>
    /// CanvasProp.xaml 的交互逻辑
    /// </summary>
    public partial class CanvasProp : UserControl
    {
        public CanvasProp()
        {
            InitializeComponent();
        }
        public Canvas gCanvas { get; private set; }
        public UIItem gUIItem { get; private set; }

        internal void Bind(MainWindow mainWindow, Canvas x)
        {
            gCanvas = x;
            gUIItem = (UIItem)mainWindow.GeItemByTag((string)x.Tag); 
            localrelate.SetItm(mainWindow, gUIItem, x);
            TextBlockTag.Text = x.Tag.ToString();
            TextBoxW.Text = x.Width.ToString();
            TextBoxH.Text = x.Height.ToString();
            x.SetBinding(Image.WidthProperty, new Binding("Text") { Source = TextBoxW });
            x.SetBinding(Image.HeightProperty, new Binding("Text") { Source = TextBoxH });
            X.Text = x.GetValue(Canvas.LeftProperty).ToString();
            Y.Text = x.GetValue(Canvas.TopProperty).ToString();
            Binding xxx = new Binding("Text") { Source = X };
            xxx.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.LeftProperty, xxx);
            Binding yyy = new Binding("Text") { Source = Y };
            yyy.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.TopProperty, yyy);
            TextBoxName.Text = x.Name.ToString();
            x.SetBinding(Image.NameProperty, new Binding("Text") { Source = TextBoxName });
        }

        private void Button_LoadXaml(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "Xaml Files (*.Xaml)|*.Xaml"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                StreamReader sRead = new StreamReader(openFileDialog.FileName);
                string nowPC = sRead.ReadToEnd();
                sRead.Close();
                ((ItemCanvas)gUIItem).ItemBin = nowPC;
                StringReader stringReader = new StringReader(nowPC);
                XmlReader xmlReader = XmlReader.Create(stringReader);
                Canvas xxx = (Canvas)System.Windows.Markup.XamlReader.Load(xmlReader);
                gCanvas.Children.Clear();
                gCanvas.Children.Add(xxx);
            }
        }
    }
}